﻿
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports elpmis

Namespace httpFunctions

    'parameters, NA
    'return, var, a pointer of a new xhtmlpage, xhtmlpage/xmlcontrol is not cloneable, so do not need to use pointer
    Public Class xhtmlpagecreator
        Inherits internalFunction

        Public Class parameters
        End Class

        Public Sub New()
            MyBase.New("xhtmlpage.create")
        End Sub

        Public Shared Function create_bare_variable(ByVal page As xhtmlpage) As bare_variable
            Return New const_bare_variable(typeDefination.var, page)
        End Function

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            output = create_bare_variable(currentContext().createControl(New xhtmlpage()))
            Return True
        End Function
    End Class

    'parameters, page, a pointer of an xhtmlpage
    'return, var, a pointer of a child of xhtmlpage
    Public MustInherit Class xhtmlpagechild
        Inherits internalFunction

        Public Class parameters
            Public Const page As String = "page"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.page)
        End Sub

        Protected Sub New(ByVal name As String)
            MyBase.New(name, m)
        End Sub

        Protected MustOverride Function child(ByVal i As xhtmlpage) As xmlcontrol

        Protected NotOverridable Overrides Function invoke(ByVal ctx As context, _
                                                           ByRef output As bare_variable) As Boolean
            Dim page As xhtmlpage = Nothing
            If ctx.findVariable(Of xhtmlpage)(parameters.page, page) Then
                output = New bare_variable(typeDefination.var, child(page))
                Return True
            Else
                raiseError("expect parameter " + parameters.page, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class

    'parameters, same as xhtmlpagechild
    'return, var, a pointer of the title of xhtmlpage
    Public Class xhtmlpagetitle
        Inherits xhtmlpagechild

        Public Sub New()
            MyBase.New("xhtmlpage.title")
        End Sub

        Protected Overrides Function child(ByVal i As xhtmlpage) As xmlcontrol
            Return i.title()
        End Function
    End Class

    'parameters, same as xhtmlpagechild
    'return, var, a pointer of the html of xhtmlpage
    Public Class xhtmlpagehtml
        Inherits xhtmlpagechild

        Public Sub New()
            MyBase.New("xhtmlpage.html")
        End Sub

        Protected Overrides Function child(ByVal i As xhtmlpage) As xmlcontrol
            Return i.html()
        End Function
    End Class

    'parameters, same as xhtmlpagechild
    'return, var, a pointer of the head of xhtmlpage
    Public Class xhtmlpagehead
        Inherits xhtmlpagechild

        Public Sub New()
            MyBase.New("xhtmlpage.head")
        End Sub

        Protected Overrides Function child(ByVal i As xhtmlpage) As xmlcontrol
            Return i.head()
        End Function
    End Class

    'parameters, same as xhtmlpagechild
    'return, var, a pointer of the body of xhtmlpage
    Public Class xhtmlpagebody
        Inherits xhtmlpagechild

        Public Sub New()
            MyBase.New("xhtmlpage.body")
        End Sub

        Protected Overrides Function child(ByVal i As xhtmlpage) As xmlcontrol
            Return i.body()
        End Function
    End Class

    'parameters, same as xhtmlpagechild
    'return, var, a pointer of the inlineScript of xhtmlpage
    Public Class xhtmlpageinlinescript
        Inherits xhtmlpagechild

        Public Sub New()
            MyBase.New("xhtmlpage.script")
        End Sub

        Protected Overrides Function child(ByVal i As xhtmlpage) As xmlcontrol
            Return i.inlineScript()
        End Function
    End Class

    'parameters, same as xhtmlpagechild
    'return, var, a pointer of the inlineStyle of xhtmlpage
    Public Class xhtmlpageinlinestyle
        Inherits xhtmlpagechild

        Public Sub New()
            MyBase.New("xhtmlpage.style")
        End Sub

        Protected Overrides Function child(ByVal i As xhtmlpage) As xmlcontrol
            Return i.inlineStyle()
        End Function
    End Class

    'parameters, same as xhtmlpagechild
    'return, var, a pointer of the footer div of xhtmlpage
    Public Class xhtmlpagefooter
        Inherits xhtmlpagechild

        Public Sub New()
            MyBase.new("xhtmlpage.footer")
        End Sub

        Protected Overrides Function child(ByVal i As xhtmlpage) As xmlcontrol
            Return i.footer()
        End Function
    End Class

    'parameters, page, input xhtmlpage
    '            type, optional, any, convert to string
    '            name, any, convert to string
    '            content, any, convert to string
    'return, bool, success or not
    Public MustInherit Class xhtmlpageinlineresource
        Inherits internalFunction

        Public Class parameters
            Public Const page As String = "page"
            Public Const type As String = "type"
            Public Const name As String = "name"
            Public Const content As String = "content"
            Public Const crunch As String = "crunch"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.page, parameters.name, parameters.content, _
                           parameters.crunch, parameters.type)
        End Sub

        Protected Sub New(ByVal name As String)
            Me.New(name, m)
        End Sub

        Protected Sub New(ByVal name As String, ByVal m As iParametersMapping)
            MyBase.New(name, m)
        End Sub

        Protected Overridable Function nopage() As Boolean
            Return False
        End Function

        Protected MustOverride Function handleresource(ByVal page As xhtmlpage, ByVal type As String, _
                                                       ByVal name As String, ByVal content As String, _
                                                       ByVal crunch As Boolean) As Boolean
        Protected MustOverride Function handleresource(ByVal page As xhtmlpage, ByVal name As String, _
                                                       ByVal content As String, ByVal crunch As Boolean) As Boolean

        Protected NotOverridable Overrides Function invoke(ByVal ctx As context, _
                                                           ByRef output As bare_variable) As Boolean
            Dim page As xhtmlpage = Nothing
            If nopage() OrElse ctx.findVariable(Of xhtmlpage)(parameters.page, page) Then
                Dim type As bare_variable = Nothing
                type = ctx.findVariable(parameters.type)
                Dim name As bare_variable = Nothing
                name = ctx.findVariable(parameters.name)
                If name Is Nothing Then
                    raiseError("expect parameter " + parameters.name, errorHandle.errorType.user)
                    Return False
                Else
                    Dim content As String = Nothing
                    content = ctx.findVariableStr(parameters.content)
                    Dim crunch As Boolean
                    If Not ctx.findVariableBool(parameters.crunch, crunch) Then
                        crunch = xmlcontrol.shouldCompress()
                    End If
                    Dim rtn As Boolean = False
                    If type Is Nothing Then
                        rtn = handleresource(page, Convert.ToString(name.value()), _
                                             content, crunch)
                    Else
                        rtn = handleresource(page, Convert.ToString(type.value()), _
                                             Convert.ToString(name.value()), content, _
                                             crunch)
                    End If

                    output = New bare_variable(typeDefination.bool, rtn)
                    Return True
                End If
            Else
                raiseError("expect parameter " + parameters.page, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class

    'parameters, same as xhtmlpageaddinlineresource
    'return, same as xhtmlpageaddinlineresource
    Public Class xhtmlpageaddinlinescript
        Inherits xhtmlpageinlineresource

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal name As String, _
                                                    ByVal content As String, ByVal crunch As Boolean) As Boolean
            Return page.inlineScript().addscript(name, content)
        End Function

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal type As String, _
                                                    ByVal name As String, ByVal content As String, _
                                                    ByVal crunch As Boolean) As Boolean
            Return page.inlineScript().addscript(type, name, content)
        End Function

        Public Sub New()
            MyBase.New("xhtmlpage.addscript")
        End Sub
    End Class

    'parameters, same as xhtmlpageaddinlineresource
    'return, same as xhtmlpageaddinlineresource
    Public Class xhtmlpageaddendinlinescript
        Inherits xhtmlpageinlineresource

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal name As String, _
                                                    ByVal content As String, ByVal crunch As Boolean) As Boolean
            Return page.endInlineScript().addscript(name, content)
        End Function

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal type As String, _
                                                    ByVal name As String, ByVal content As String, _
                                                    ByVal crunch As Boolean) As Boolean
            Return page.endInlineScript().addscript(type, name, content)
        End Function

        Public Sub New()
            MyBase.New("xhtmlpage.addendscript")
        End Sub
    End Class

    'parameters, same as xhtmlpageaddinlineresource
    'return, same as xhtmlpageaddinlineresource
    Public Class xhtmlpageaddinlinestyle
        Inherits xhtmlpageinlineresource

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal name As String, _
                                                    ByVal content As String, ByVal crunch As Boolean) As Boolean
            Return page.inlineStyle().addstyle(name, content, crunch)
        End Function

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal type As String, _
                                                    ByVal name As String, ByVal content As String, _
                                                    ByVal crunch As Boolean) As Boolean
            Return page.inlineStyle().addstyle(type, name, content, crunch)
        End Function

        Public Sub New()
            MyBase.New("xhtmlpage.addstyle")
        End Sub
    End Class

    Public Class xhtmlpageaddregisteredinlinescript
        Inherits xhtmlpageinlineresource

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal name As String, _
                                                    ByVal content As String, ByVal crunch As Boolean) As Boolean
            Return page.inlineScript().addRegisteredScript(name)
        End Function

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal type As String, _
                                                    ByVal name As String, ByVal content As String, _
                                                    ByVal crunch As Boolean) As Boolean
            Return page.inlineScript().addRegisteredScript(type, name)
        End Function

        Public Sub New()
            MyBase.New("xhtmlpage.registered.addscript")
        End Sub
    End Class

    Public Class xhtmlpageaddregisteredinlinestyle
        Inherits xhtmlpageinlineresource

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal name As String, _
                                                    ByVal content As String, ByVal crunch As Boolean) As Boolean
            Return page.inlineStyle().addRegisteredStyle(name)
        End Function

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal type As String, _
                                                    ByVal name As String, ByVal content As String, _
                                                    ByVal crunch As Boolean) As Boolean
            Return page.inlineStyle().addRegisteredStyle(type, name)
        End Function

        Public Sub New()
            MyBase.New("xhtmlpage.registered.addstyle")
        End Sub
    End Class

    Public Class registerinlinescript
        Inherits xhtmlpageinlineresource

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.name, parameters.content, parameters.type, parameters.crunch)
        End Sub

        Protected Overrides Function nopage() As Boolean
            Return True
        End Function

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal name As String, _
                                                    ByVal content As String, ByVal crunch As Boolean) As Boolean
            Return inlineScript.registerScript(name, content)
        End Function

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal type As String, _
                                                    ByVal name As String, ByVal content As String, _
                                                    ByVal crunch As Boolean) As Boolean
            Return inlineScript.registerScript(type, name, content)
        End Function

        Public Sub New()
            MyBase.new("registerscript", m)
        End Sub
    End Class

    Public Class registerinlinestyle
        Inherits xhtmlpageinlineresource

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.name, parameters.content, parameters.type, parameters.crunch)
        End Sub

        Protected Overrides Function nopage() As Boolean
            Return True
        End Function

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal name As String, _
                                                    ByVal content As String, ByVal crunch As Boolean) As Boolean
            Return inlineStyle.registerStyle(name, content, crunch)
        End Function

        Protected Overrides Function handleresource(ByVal page As xhtmlpage, ByVal type As String, _
                                                    ByVal name As String, ByVal content As String, _
                                                    ByVal crunch As Boolean) As Boolean
            Return inlineStyle.registerStyle(type, name, content, crunch)
        End Function

        Public Sub New()
            MyBase.new("registerstyle", m)
        End Sub
    End Class

    'parmaeters, page, var, xhtmlpage instance
    '            link, any, convert to string, the link of css file
    'return, bool, success or not
    Public MustInherit Class xhtmlpageaddlink
        Inherits internalFunction

        Public Class parameters
            Public Const page As String = "page"
            Public Const link As String = "link"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.page, parameters.link)
        End Sub

        Protected Sub New(ByVal name As String)
            MyBase.New(name, m)
        End Sub

        Protected MustOverride Sub addlink(ByVal page As xhtmlpage, ByVal link As String)

        Protected NotOverridable Overrides Function invoke(ByVal ctx As context, _
                                                           ByRef output As bare_variable) As Boolean
            Dim page As xhtmlpage = Nothing
            If ctx.findVariable(Of xhtmlpage)(parameters.page, page) Then
                Dim link As String = Nothing
                If ctx.findVariableStr(parameters.link, link) Then
                    If Not isemptystring(link) Then
                        addlink(page, link)
                    End If
                    output = New bare_variable(typeDefination.bool, True)
                    Return True
                Else
                    raiseError("expect parameter " + parameters.link, errorHandle.errorType.user)
                    Return False
                End If
            Else
                raiseError("expect parameter " + parameters.page, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class

    'same as xhtmlpageaddlink
    Public Class xhtmlpageaddcsslink
        Inherits xhtmlpageaddlink

        Public Sub New()
            MyBase.New("xhtmlpage.addcsslink")
        End Sub

        Protected Overrides Sub addlink(ByVal page As xhtmlpage, ByVal link As String)
            page.addcsslink(link)
        End Sub
    End Class

    'same as xhtmlpageaddlink
    Public Class xhtmlpageaddjslink
        Inherits xhtmlpageaddlink

        Public Sub New()
            MyBase.New("xhtmlpage.addjslink")
        End Sub

        Protected Overrides Sub addlink(ByVal page As xhtmlpage, ByVal link As String)
            page.addjsscript(link)
        End Sub
    End Class

    Public Class inlineResourceContent
        Inherits inputIFunction(Of inlineResource, String)

        Public Sub New()
            MyBase.New("inlineResource.content")
        End Sub

        Protected Overrides Function finder() As iVariableFinder(Of inlineResource)
            Return inlineResource_finder()
        End Function

        Protected Overrides Function invoke(ByVal i As inlineResource, ByRef o As String) As Boolean
            o = inlineResource.resourceContent(i)
            Return True
        End Function

        Protected Overrides Function returnType() As typeDefination
            Return typeDefination.string
        End Function
    End Class

    Public Class xhtmlpagebackgroundDownloaderAdd
        Inherits internalFunction

        Public Class parameters
            Public Const page As String = "page"
            Public Const link As String = "link"
        End Class

        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialMapping(m, parameters.page, parameters.link)
        End Sub

        Public Sub New()
            MyBase.New("xhtmlpage.backgroundDownloader.add", m)
        End Sub

        Protected Overrides Function invoke(ByVal ctx As context, ByRef output As bare_variable) As Boolean
            Dim page As xhtmlpage = Nothing
            If ctx.findVariable(Of xhtmlpage)(parameters.page, page) Then
                Dim link As String = Nothing
                If ctx.findVariableStr(parameters.link, link) Then
                    page.backgroundDownloader().add(link)
                    Return True
                Else
                    raiseError("expect parameter " + parameters.link, errorHandle.errorType.user)
                    Return False
                End If
            Else
                raiseError("expect parameter " + parameters.page, errorHandle.errorType.user)
                Return False
            End If
        End Function
    End Class
End Namespace
